package com.oym.oa.controller;

import com.oym.oa.biz.IDepartmentBiz;
import com.oym.oa.entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;
@Controller("departmentController")
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentBiz departmentBiz;

    @RequestMapping("/list")
    public String list(Map<String,Object> map) {
        map.put("list",departmentBiz.getAll());
        return "department_list";
    }

    @RequestMapping("/to_add")
    public String toAdd(Map<String,Object> map) {
        map.put("department",new Department());
        return "department_add";
    }

    @RequestMapping("/add")
    public String add(Department department) {

        if ("".equals(department.getSn().trim()) || "".equals(department.getAddress().trim())
                || "".equals(department.getName().trim())) {
            return "redirect:list";
        }

        try {
            departmentBiz.add(department);
        }catch (Exception e) {
            return "redirect:list";
        }
        return "redirect:list";
    }

    @RequestMapping(value = "/to_update",params = "sn")
    public String toUpdate(String sn,Map<String,Object> map) {
        map.put("department",departmentBiz.get(sn));
        return "department_update";
    }

    @RequestMapping("/update")
    public String update(Department department) {

        if ("".equals(department.getSn().trim()) || "".equals(department.getAddress().trim())
                || "".equals(department.getName().trim())) {
            return "redirect:list";
        }

        departmentBiz.edit(department);
        return "redirect:list";
    }

    @RequestMapping(value = "/remove",params = "sn")
    public String remove(String sn) {
        departmentBiz.remove(sn);
        return "redirect:list";
    }

}
